package com.example.diagnose.Service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.diagnose.Domain.Article;
import com.example.diagnose.Domain.FavoriteArticle;
import com.example.diagnose.Mapper.ArticleMapper;
import com.example.diagnose.Mapper.FavoriteArticleMapper;
import com.example.diagnose.Service.ArticleService;
import com.example.diagnose.Service.FavoriteArticleService;
import jakarta.annotation.Resource;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

@Service
public class FavoriteArticleServiceImpl extends ServiceImpl<FavoriteArticleMapper, FavoriteArticle> implements FavoriteArticleService {

    @Resource
    FavoriteArticle favoriteArticle;

    @Resource
    FavoriteArticleMapper favoriteArticleMapper;

    @Resource
    ArticleService articleService;


    @Override
    public int addFavoriteArticle(FavoriteArticle favoriteArticle) {
        return favoriteArticleMapper.insert(favoriteArticle);
    }

    @Override
    public int deleteFavoriteArticle(int favorite_id) {
        FavoriteArticle favoriteArticle = new FavoriteArticle();
        favoriteArticle.setFavorite_id(favorite_id);
        return favoriteArticleMapper.deleteById(favoriteArticle);
    }

    @Override
    public List<Article> getFavoriteArticleByAccount(String account) {
        QueryWrapper<FavoriteArticle> wrapper = new QueryWrapper<>();
        wrapper.eq("user_account", account);
        List<FavoriteArticle> favoriteArticleList= favoriteArticleMapper.selectList(wrapper);

        List<Article> articleList = new ArrayList<>();
        for(FavoriteArticle favoriteArticle: favoriteArticleList){
            Article article1 = articleService.getArticleById(favoriteArticle.getArticle_id());
            articleList.add(article1);
        }
        return articleList;
    }
}
